2000W / 48V LLC Battery-Charger  V1.00
xmc_common.c
Go to the documentation of this file.
1 
50 #include "xmc_common.h"
51 
52 /*******************************************************************************
53  * DATA STRUCTURES
54  *******************************************************************************/
55 struct list
56 {
57  struct list *next;
58 };
59 
60 /*******************************************************************************
61  * API IMPLEMENTATION
62  *******************************************************************************/
63 #if defined(XMC_ASSERT_ENABLE)
64 __WEAK void XMC_AssertHandler(const char *const msg, const char *const file, uint32_t line)
65 {
66  XMC_UNUSED_ARG(msg);
67  XMC_UNUSED_ARG(file);
68  XMC_UNUSED_ARG(line);
69 
70  while(1)
71  {
72  /* Endless loop */
73  }
74 }
75 #endif
76 
78 {
79  *list = NULL;
80 }
81 
83 {
84  return *list;
85 }
86 
88 {
89  struct list *tail;
90 
91  if (*list == NULL)
92  {
93  tail = NULL;
94  }
95  else
96  {
97  for (tail = (struct list *)*list; tail->next != NULL; tail = tail->next)
98  {
99  /* Loop through the list */
100  }
101  }
102 
103  return tail;
104 }
105 
106 void XMC_LIST_Add(XMC_LIST_t *list, void *item)
107 {
108  struct list *tail;
109 
110  ((struct list *)item)->next = NULL;
111  tail = (struct list *)XMC_LIST_GetTail(list);
112 
113  if (tail == NULL)
114  {
115  *list = item;
116  }
117  else
118  {
119  tail->next = (struct list *)item;
120  }
121 }
122 
123 void XMC_LIST_Remove(XMC_LIST_t *list, void *item)
124 {
125  struct list *right, *left;
126 
127  if (*list != NULL)
128  {
129  left = NULL;
130  for(right = (struct list *)*list; right != NULL; right = right->next)
131  {
132  if(right == item)
133  {
134  if(left == NULL)
135  {
136  /* First on list */
137  *list = right->next;
138  }
139  else
140  {
141  /* Not first on list */
142  left->next = right->next;
143  }
144  right->next = NULL;
145  break;
146  }
147  left = right;
148  }
149  }
150 }
151 
152 void XMC_LIST_Insert(XMC_LIST_t *list, void *prev_item, void *new_item)
153 {
154  if (prev_item == NULL)
155  {
156  ((struct list *)new_item)->next = (struct list *)*list;
157  *list = new_item;
158  }
159  else
160  {
161  ((struct list *)new_item)->next = ((struct list *)prev_item)->next;
162  ((struct list *)prev_item)->next = (struct list *)new_item;
163  }
164 }
165 
167 {
168  XMC_ASSERT("XMC_PRIOARRAY_Init: NULL pointer", prioarray != NULL);
169 
170  /* Initialize head, next points to tail, previous to NULL and the priority is MININT */
171  prioarray->items[prioarray->size].next = prioarray->size + 1;
172  prioarray->items[prioarray->size].previous = -1;
173  prioarray->items[prioarray->size].priority = INT32_MAX;
174 
175  /* Initialize tail, next points to NULL, previous is the head and the priority is MAXINT */
176  prioarray->items[prioarray->size + 1].next = -1;
177  prioarray->items[prioarray->size + 1].previous = prioarray->size;
178  prioarray->items[prioarray->size + 1].priority = INT32_MIN;
179 
180 }
181 
182 void XMC_PRIOARRAY_Add(XMC_PRIOARRAY_t *prioarray, int32_t item, int32_t priority)
183 {
184  int32_t next;
185  int32_t previous;
186 
187  XMC_ASSERT("XMC_PRIOARRAY_Add: item out of range", (item >= 0) && (item < prioarray->size));
188 
189  next = XMC_PRIOARRAY_GetHead(prioarray);
190  while (XMC_PRIOARRAY_GetItemPriority(prioarray, next) > priority)
191  {
192  next = XMC_PRIOARRAY_GetItemNext(prioarray, next);
193  }
194 
195  previous = prioarray->items[next].previous;
196 
197  prioarray->items[item].next = next;
198  prioarray->items[item].previous = previous;
199  prioarray->items[item].priority = priority;
200 
201  prioarray->items[previous].next = item;
202  prioarray->items[next].previous = item;
203 }
204 
205 void XMC_PRIOARRAY_Remove(XMC_PRIOARRAY_t *prioarray, int32_t item)
206 {
207  int32_t next;
208  int32_t previous;
209 
210  XMC_ASSERT("XMC_PRIOARRAY_Add: item out of range", (item >= 0) && (item < prioarray->size));
211 
212  next = prioarray->items[item].next;
213  previous = prioarray->items[item].previous;
214 
215  prioarray->items[previous].next = next;
216  prioarray->items[next].previous = previous;
217 }
218 
219 
void XMC_LIST_Remove(XMC_LIST_t *list, void *item)
Definition: xmc_common.c:123
void XMC_PRIOARRAY_Add(XMC_PRIOARRAY_t *prioarray, int32_t item, int32_t priority)
Definition: xmc_common.c:182
#define __WEAK
Definition: cmsis_gcc.h:59
int32_t size
Definition: xmc_common.h:186
#define XMC_ASSERT(msg, exp)
Definition: xmc_common.h:132
void XMC_LIST_Init(XMC_LIST_t *list)
Definition: xmc_common.c:77
__STATIC_INLINE int32_t XMC_PRIOARRAY_GetItemPriority(XMC_PRIOARRAY_t *prioarray, int32_t item)
Definition: xmc_common.h:274
#define XMC_UNUSED_ARG(x)
Definition: xmc_common.h:142
XMC_PRIOARRAY_ITEM_t * items
Definition: xmc_common.h:187
void XMC_PRIOARRAY_Init(XMC_PRIOARRAY_t *prioarray)
Definition: xmc_common.c:166
__STATIC_INLINE int32_t XMC_PRIOARRAY_GetItemNext(XMC_PRIOARRAY_t *prioarray, int32_t item)
Definition: xmc_common.h:283
void XMC_PRIOARRAY_Remove(XMC_PRIOARRAY_t *prioarray, int32_t item)
Definition: xmc_common.c:205
__STATIC_INLINE int32_t XMC_PRIOARRAY_GetHead(XMC_PRIOARRAY_t *prioarray)
Definition: xmc_common.h:256
void XMC_LIST_Insert(XMC_LIST_t *list, void *prev_item, void *new_item)
Definition: xmc_common.c:152
void * XMC_LIST_GetHead(XMC_LIST_t *list)
Definition: xmc_common.c:82
void * XMC_LIST_t
Definition: xmc_common.h:169
struct list * next
Definition: xmc_common.c:57
void * XMC_LIST_GetTail(XMC_LIST_t *list)
Definition: xmc_common.c:87
void XMC_AssertHandler(const char *const msg, const char *const file, uint32_t line)
void XMC_LIST_Add(XMC_LIST_t *list, void *item)
Definition: xmc_common.c:106